package com.loadmore.common.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.loadmore.common.adapter.callback.RVBaseClickListener;
import com.loadmore.common.adapter.callback.RVBaseLongClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunleilei on 2017/3/7
 */

public abstract class RVBaseAdapter extends RecyclerView.Adapter<RVBaseHolder> {
    protected Context context;
    public List items;
    public boolean order;
    private RVBaseClickListener clickListener;
    private RVBaseLongClickListener longClickListener;
    protected LayoutInflater inflater;

    public abstract RVBaseHolder createHolder(ViewGroup parent, int viewType);

    public abstract void bindHolder(RVBaseHolder holder, int position);

    public RVBaseAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        items = new ArrayList();
    }

    public void append(List items) {
        this.items.addAll(items);
        notifyItemRangeInserted(this.items.size(), this.items.size() + items.size());
    }

    public void appendItem(Object object) {
        this.items.add(object);
        notifyItemInserted(this.items.size());
    }

    public void appendItem(Object object, int position) {
        this.items.add(position, object);
        notifyItemInserted(position);
    }

    public void append2Head(Object object) {
        this.items.add(0, object);
        notifyItemInserted(0);
        notifyDataSetChanged();
    }

    public void remove(Object object) {
        this.items.remove(object);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        this.items.remove(position);
        notifyItemRemoved(position);
    }

    public void clear() {
        int size = this.items.size();
        this.items.clear();
        notifyItemRangeRemoved(0,size);
    }

    public void setClickListener(RVBaseClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setLongClickListener(RVBaseLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    @Override
    public RVBaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return createHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(final RVBaseHolder holder, int position) {
        bindHolder(holder, holder.getLayoutPosition());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onClick(holder.getAdapterPosition());
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (longClickListener != null) {
                    longClickListener.onLongClick(holder.getAdapterPosition());
                }
                return true;
            }

        });

    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public Object getItem(int position) {
        return items.get(position);
    }
}
